import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/views/components/Layout.vue'
import Header from '@/views/components/Header.vue'
import Main from '@/views/components/Main.vue'
import Home from '@/views/Home.vue'
import Login from '@/views/Login.vue'
import AdminList from '@/views/admin/AdminList.vue'
import BuildingList from '@/views/building/BuildingList.vue'
import RoomList from '@/views/room/RoomList.vue'
import ResidentList from '@/views/resident/ResidentList.vue'
import ChargeList from '@/views/charge/ChargeList.vue'
import ParkingAreaList from '@/views/parkingArea/ParkingAreaList.vue'
import ParkingChargeList from '@/views/parkingCharge/ParkingChargeList.vue'
import RepairRequestList from '@/views/repairRequest/RepairRequestList.vue'
import StaffList from '@/views/staff/StaffList.vue'
import FacilityList from '@/views/facility/FacilityList.vue'
import FacilityMaintenanceRecordList from '@/views/facilityMaintenanceRecord/FacilityMaintenanceRecordList.vue'
import ChartList from '@/views/chart/ChartList.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '',
        components: {
          default: Main,
          header: Header
        },
        children: [
          {
            path: '/Home',
            name: 'Home',
            component: Home
          },
          {
            path: '/AdminList',
            name: 'AdminList',
            component: AdminList
          },
          {
            path: '/BuildingList',
            name: 'BuildingList',
            component: BuildingList
          },
          {
            path: '/RoomList',
            name: 'RoomList',
            component: RoomList
          },
          {
            path: '/ResidentList',
            name: 'ResidentList',
            component: ResidentList
          },
          {
            path: '/ChargeList',
            name: 'ChargeList',
            component: ChargeList
          },
          {
            path: '/ParkingAreaList',
            name: 'ParkingAreaList',
            component: ParkingAreaList
          },
          {
            path: '/ParkingChargeList',
            name: 'ParkingChargeList',
            component: ParkingChargeList
          },
          {
            path: '/RepairRequestList',
            name: 'RepairRequestList',
            component: RepairRequestList
          },
          {
            path: '/StaffList',
            name: 'StaffList',
            component: StaffList
          },
          {
            path: '/FacilityList',
            name: 'FacilityList',
            component: FacilityList
          },
          {
            path: '/FacilityMaintenanceRecordList',
            name: 'FacilityMaintenanceRecordList',
            component: FacilityMaintenanceRecordList
          },
          {
            path: '/ChartList',
            name: 'ChartList',
            component: ChartList
          }
        ]
      }
    ]
  },
  {
    path: '/Login',
    name: 'Login',
    component: Login
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.name !== 'Login' && to.name !== 'VendorRegister' && !sessionStorage.getItem('backendToken')) {
    next('/Login')
  } else {
    next()
  }
})

export default router
